
#include <unistd.h>
#include <string>

#include "Log.h"
#include "BasicDevice.h"
#include "common.h"
#include "ErrorCode.h"

BasicDevice::BasicDevice(const std::string& deviceName, const int oflags, const int modes) : deviceName(deviceName), moflags(oflags), mmodes(modes) {
    deviceFd = openFd(deviceName, moflags, mmodes);
}


int BasicDevice::openFd(const std::string& deviceName, const int oflags, const int modes){
    int fd = ::open(deviceName.data(), oflags, modes);
    if(fd < 0) {
        std::string prefix = deviceName;
        prefix.append(",open error");
        LOG_ERROR(buildErrorMessage(fd, errno, prefix).data());
    }

    return fd;
}

bool BasicDevice::closeFd(const int fd){
    return 0 == ::close(fd);
}


BasicDevice::~BasicDevice() {
    close();
}
